<template>
  <div class="container">
    <template>
      <!--顶部操作表单-->
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item>
          <!--添加学生-->
          <el-button type="primary" @click="addStudent">添加学生</el-button>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="listQuery.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="listQuery.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="学院">
          <el-select v-model="listQuery.collegeId" :clearable="1===1" placeholder="请选择学院" @change="changeSelect">
            <el-option
              v-for="item in collegeOptions"
              :key="item.id"
              :label="item.collegeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级">
          <el-select v-model="listQuery.classId" :clearable="1===1" placeholder="请选择班级">
            <el-option
              v-for="item in classOptions"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>

      </el-form>
      <!--顶部操作表单-->
      <!-- 添加表单-->
      <el-dialog title="添加学生" :visible="addDialogFormVisible" top="30px" :show-close="false">
        <el-form :model="QueryForEdit" ref="form" :rules="rules">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
            <el-input v-model="QueryForEdit.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
            <el-input v-model="QueryForEdit.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="QueryForEdit.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="QueryForEdit.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
            <el-input v-model="QueryForEdit.phone" autocomplete="off"></el-input>
          </el-form-item>
          <!--        <el-form-item label="学院名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.collegeName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="学院名称" :label-width="formLabelWidth" prop="collegeId">
            <el-select v-model="QueryForEdit.collegeId" placeholder="请选择学院" @change="changeSelect">
              <el-option
                v-for="item in collegeOptions"
                :key="item.id"
                :label="item.collegeName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="班级名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.className" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="班级名称" :label-width="formLabelWidth" prop="classId">
            <el-select v-model="QueryForEdit.classId" placeholder="请选择班级">
              <el-option
                v-for="item in classOptions"
                :key="item.id"
                :label="item.className"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="权限" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.powerName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="权限" :label-width="formLabelWidth" prop="power">
            <el-select v-model="QueryForEdit.power" placeholder="权限">
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.powerName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addDialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClick('form')">确 定</el-button>
        </div>
      </el-dialog>
      <!--修改表单-->
      <el-dialog title="添加学生" :visible="editDialogFormVisible" top="30px" :show-close="false">
        <el-form :model="QueryForEdit" ref="form">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
            <el-input v-model="QueryForEdit.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
            <el-input v-model="QueryForEdit.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="QueryForEdit.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="age">
            <el-input v-model="QueryForEdit.phone" autocomplete="off"></el-input>
          </el-form-item>
          <!--        <el-form-item label="学院名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.collegeName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="学院名称" :label-width="formLabelWidth" prop="collegeId">
            <el-select v-model="QueryForEdit.collegeId" placeholder="请选择学院" @change="changeSelect">
              <el-option
                v-for="item in collegeOptions"
                :key="item.id"
                :label="item.collegeName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="班级名称" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.className" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="班级名称" :label-width="formLabelWidth" prop="classId">
            <el-select v-model="QueryForEdit.classId" placeholder="请选择班级">
              <el-option
                v-for="item in classOptions"
                :key="item.id"
                :label="item.className"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="权限" :label-width="formLabelWidth">
                    <el-input v-model="QueryForEdit.powerName" autocomplete="off"></el-input>
                  </el-form-item>-->
          <el-form-item label="权限" :label-width="formLabelWidth" prop="power">
            <el-select v-model="QueryForEdit.power" placeholder="权限">
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.powerName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClick('form')">确 定</el-button>
        </div>
      </el-dialog>
      <!--学生列表-->
      <el-card class="box-card">
        <el-table
          ref="multipleTable"
          :data="tableData"
          v-loading="loading"
          @selection-change="handleSelectionChange"
          border
          style="width: 100%">
          <el-table-column
            prop="username"
            label="账号"
            width="180">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            width="180">
          </el-table-column>
          <el-table-column
            prop="age"
            label="年龄"
            width="180"
          >
          </el-table-column>
          <el-table-column
            prop="powerName"
            label="角色"
            width="180"
          >
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话号码"
            width="180"
          >
          </el-table-column>
            <el-table-column
              prop="email"
              label="邮箱"
              width="180"
            >
            </el-table-column>
          <el-table-column
            prop="collegeName"
            label="学院名称"
            width="180"
          >
          </el-table-column>
          <el-table-column
            prop="className"
            label="班级名称"
            width="180"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="180">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" circle @click="handleEditClick(scope.row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" circle @click="handleDeleteClick(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="listQuery.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-card>
    </template>
  </div>
</template>

<script>
  import {getStudentList, addStudent, deleteStu} from "../../api/student";
  import {geClassdropDownList, getCollegedropDownList, getRoledropDownList} from "../../api/common";

  export default {
    name: "",
    data() {
      return {
        test: "1.完成\n2.完成",
        tableData: [],
        addDialogFormVisible: false,
        editDialogFormVisible:false,
        formLabelWidth: '120px',
        loading: false,
        total: 0,
        addForm: {
          name: '',
          username: '',
          password: '',
          age: '',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        classOptions: [],
        collegeOptions: [],
        roleOptions: [],
        QueryForEdit: {
          name: '',
          username: '',
          password: '',
          age: '',
          email:'',
          phone:'',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: ''
        },
        listQuery: {
          page: 1,
          size: 5,
          username: '',
          name: '',
          classId: '',
          collegeId: '',
          id: ''
        },
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          username: [
            {required: true, message: '请输入账号', trigger: 'blur'}
          ],
          collegeId: [
            {required: true, message: '请选择学院', trigger: 'blur'}
          ],
          classId: [
            {required: true, message: '请选择班级', trigger: 'blur'}
          ],
          power: [
            {required: true, message: '请选择权限', trigger: 'blur'}
          ],
          age: [
            {required: true, message: '请输入年龄', trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入电话', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入年龄', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      // 获取学生列表
      getStudentList() {
        this.loading=true
        getStudentList(this.listQuery).then(res => {
          this.tableData = res.data.pageList;
          this.total = res.data.total
          console.log(res)
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })

      },
      //添加和修改的方法
      addClick(formName) {

        this.$refs[formName].validate((valid) => {
          if (valid||this.QueryForEdit.typeForEdit === 'edit') {
            this.loading=true
            addStudent(this.QueryForEdit).then(res => {
              if (this.QueryForEdit.typeForEdit === 'add') {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                });
                this.editDialogFormVisible=false
              } else {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                this.editDialogFormVisible=false
              }
              this.addDialogFormVisible = false;
              this.OnClose()
              this.getStudentList()
              this.loading=false
            }).catch(err=>{
              this.loading=false
            })
          } else {
            this.$message.warning("请完善信息")
          }
          x
        })

      },
      //构造修改条件
      handleEditClick(param) {
        this.QueryForEdit.name = param.name
        this.QueryForEdit.age = param.age
        this.QueryForEdit.username = param.username
        this.QueryForEdit.collegeId = param.collegeId
        this.QueryForEdit.classId = param.classId
        this.QueryForEdit.email=param.email
        this.QueryForEdit.phone=param.phone
        this.QueryForEdit.id = param.id
        this.QueryForEdit.typeForEdit = 'edit'
        this.QueryForEdit.power=param.power
        this.editDialogFormVisible = true
        this.getdropDownListForCollege()
        this.getdropDownListForClass(param.collegeId)
        this.getDropdownListForRole()

      },
      //构造添加条件
      addStudent() {
        this.addDialogFormVisible = true
        this.QueryForEdit.typeForEdit = 'add'
        this.getdropDownListForCollege()
        this.getDropdownListForRole()
      },
      changeSelect() {
        this.getdropDownListForClass()
      },
      //获取学院列表
      getdropDownListForCollege() {
        getCollegedropDownList(1484744242220589057)
          .then(res => {
            console.log(res)
            this.collegeOptions = res.data
          })
      },

      //获取班级列表
      getdropDownListForClass() {
        if (this.addDialogFormVisible == true||this.editDialogFormVisible==true) {
          geClassdropDownList(this.QueryForEdit.collegeId)
            .then(res => {
              console.log(res)
              this.classOptions = res.data
            });
        } else {
          geClassdropDownList(this.listQuery.collegeId)
            .then(res => {
              console.log(res)
              this.classOptions = res.data
            });
        }

      },
      //获取权限列表
      getDropdownListForRole() {
        getRoledropDownList().then(res => {
          this.roleOptions = res.data
          this.roleOptions.shift()
          this.roleOptions.shift()
          this.roleOptions.shift()
        })
      },
      //点击查询
      onSubmit() {
        this.getStudentList()
      },
      //删除学生
      handleDeleteClick(param) {
        console.log(param)
        this.loading = true
        this.listQuery.id = param.id
        deleteStu(this.listQuery).then(res => {
          this.getStudentList()
          this.loading = false
        }).catch(err=>{
          this.loading=false
        })
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getStudentList()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getStudentList()
        console.log(`当前页: ${val}`);
      },
      //多选
      handleSelectionChange(val) {
        console.log("多选的用户")
        console.log(val)
      },
      OnClose() {
        this.addDialogFormVisible = false;
        this.QueryForEdit = {
          name: '',
          username: '',
          password: '',
          age: '',
          collegeName: '',
          className: '',
          powerName: '',
          power: '',
          classId: '',
          collegeId: '',
          typeForEdit: '',//添加是add，修改是edit
          id: ''
        }
      }
    },
    created() {
      this.getdropDownListForCollege()
      this.getStudentList()
    }
  }
</script>

<style scoped>
  .demo-form-inline {
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
